﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Linkman.Search
{
    /// <summary>
    /// index 的摘要说明。
    /// </summary>
    public partial class IndexPage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            ddl_SearchType.SelectedIndexChanged += ddl_SearchType_SelectedIndexChanged;
            btn_OK.Click += btn_OK_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                tbl_StaffSearch.Visible = true;
                tbl_LinkmanSearch.Visible = false;
                tbl_CutomSearch.Visible = false;

                BindCutomTypeList();
                BindStaffPosition();
            }

            base.OnLoad(e);
        }

        private void BindCutomTypeList()
        {
            using (var dr = MyLinkman.GetCustomLinkmanType())
            {
                ddl_CustomCatalog.DataSource = dr;
                ddl_CustomCatalog.DataTextField = "Type";
                ddl_CustomCatalog.DataValueField = "ID";
                ddl_CustomCatalog.DataBind();
            }

            ddl_CustomCatalog.Items.Insert(0, new ListItem("无限制", "0"));
        }

        private void BindStaffPosition()
        {
            ddl_StaffPosition.Items.Clear();
            foreach (var p in UDS.Components.Position.GetAllPositions())
            {
                ListItem listItem = new ListItem(p.Name, p.ID.ToString());
                ddl_StaffPosition.Items.Add(listItem);
            }

            ddl_StaffPosition.Items.Insert(0, new ListItem("无限制", "0"));
        }

        private void ddl_SearchType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ddl_SearchType.SelectedValue)
            {
                case "staff":
                    tbl_StaffSearch.Visible = true;
                    tbl_LinkmanSearch.Visible = false;
                    tbl_CutomSearch.Visible = false;
                    break;
                case "linkman":
                    tbl_StaffSearch.Visible = false;
                    tbl_LinkmanSearch.Visible = true;
                    tbl_CutomSearch.Visible = false;
                    break;
                case "custom":
                    tbl_StaffSearch.Visible = false;
                    tbl_LinkmanSearch.Visible = false;
                    tbl_CutomSearch.Visible = true;
                    break;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            var linkman = new MyLinkman();
            SqlDataReader dr_linkman;
            DataTable dt_linkman;
            string filter = "";
            switch (ddl_SearchType.SelectedValue)
            {
                case "staff":
                    dr_linkman = linkman.GetMyLinkman(1, SiteContext.Current.UserID);

                    if (tbx_StaffName.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("RealName LIKE '*" + tbx_StaffName.Text.Trim() + "*'") : filter + " and RealName LIKE '*" + tbx_StaffName.Text.Trim() + "*'";
                    }
                    if (tbx_StaffMobile.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Mobile LIKE '*" + tbx_StaffMobile.Text.Trim() + "*'") : filter + " and Mobile LIKE '*" + tbx_StaffMobile.Text.Trim() + "*'";
                    }
                    if (ddl_StaffGender.SelectedValue != "0")
                    {
                        if (ddl_StaffGender.SelectedValue == "male")
                        {
                            if (filter == "")
                                filter = "Sex=True";
                            else
                                filter += " and Sex=True";
                        }
                        else
                        {
                            if (filter == "")
                                filter = "Sex=False";
                            else
                                filter += " and Sex=False";
                        }
                    }
                    if (tbx_StaffEmail.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Email LIKE '*" + tbx_StaffEmail.Text.Trim() + "'") : filter + " and Email LIKE '*" + tbx_StaffEmail.Text.Trim() + "*'";
                    }
                    if (ddl_StaffPosition.SelectedValue != "0")
                    {
                        filter = (filter == "") ? ("Position_ID=" + ddl_StaffPosition.SelectedValue) : filter + " and Position_ID=" + ddl_StaffPosition.SelectedValue;
                    }

                    dt_linkman = Tools.ConvertDataReaderToDataTable(dr_linkman);

                    try
                    {
                        dt_linkman.DefaultView.RowFilter = filter;
                        dgrd_Staff.DataSource = dt_linkman.DefaultView;
                        dgrd_Staff.DataBind();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(filter + "   " + ex.Message);
                    }


                    dgrd_Staff.Visible = true;
                    dgrd_Linkman.Visible = false;
                    dgrd_Custom.Visible = false;
                    break;

                case "linkman":
                    dr_linkman = linkman.GetMyLinkman(2, SiteContext.Current.UserID);

                    if (tbx_LinkmanName.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Name LIKE '*" + tbx_LinkmanName.Text.Trim() + "*'") : filter + " and Name LIKE '*" + tbx_LinkmanName.Text.Trim() + "*'";
                    }
                    if (tbx_Telephone.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Telephone LIKE '*" + tbx_Telephone.Text.Trim() + "*'") : filter + " and Telephone LIKE '*" + tbx_Telephone.Text.Trim() + "*'";
                    }
                    if (ddl_LinkmanGender.SelectedValue != "0")
                    {
                        if (ddl_LinkmanGender.SelectedValue == "male")
                        {
                            if (filter == "")
                                filter = "Gender=True";
                            else
                                filter += " and Gender=True";
                        }
                        else
                        {
                            if (filter == "")
                                filter = "Gender=False";
                            else
                                filter += " and Gender=False";
                        }
                    }
                    if (tbx_LinkmanEmail.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Email LIKE '*" + tbx_LinkmanEmail.Text.Trim() + "*'") : filter + " and Email LIKE '*" + tbx_LinkmanEmail.Text.Trim() + "*'";
                    }
                    if (tbx_LinkmanUnit.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("UnitName LIKE '*" + tbx_LinkmanUnit.Text.Trim() + "*'") : filter + " AND UnitName LIKE '*" + tbx_LinkmanUnit.Text.Trim() + "*'";
                    }
                    if (tbx_LinkmanPosition.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Position LIKE '*" + tbx_LinkmanPosition.Text.Trim() + "*'") : filter + " AND Position LIKE '*" + tbx_LinkmanPosition.Text.Trim() + "*'";
                    }
                    if (tbx_LinkmanMobile.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Mobile LIKE '*" + tbx_LinkmanMobile.Text.Trim() + "*'") : filter + " AND Mobile LIKE '*" + tbx_LinkmanMobile.Text.Trim() + "*'";
                    }

                    dt_linkman = Tools.ConvertDataReaderToDataTable(dr_linkman);

                    try
                    {
                        dt_linkman.DefaultView.RowFilter = filter;
                        dgrd_Linkman.DataSource = dt_linkman.DefaultView;
                        dgrd_Linkman.DataBind();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(filter + "   " + ex.Message);
                    }


                    dgrd_Staff.Visible = false;
                    dgrd_Linkman.Visible = true;
                    dgrd_Custom.Visible = false;
                    break;

                case "custom":
                    dr_linkman = linkman.GetMyLinkman(3, SiteContext.Current.UserID);

                    if (tbx_CutomName.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Name LIKE '*" + tbx_CutomName.Text.Trim() + "*'") : filter + " and Name LIKE '*" + tbx_CutomName.Text.Trim() + "*'";
                    }

                    if (ddl_CustomGender.SelectedValue != "0")
                    {
                        if (ddl_CustomGender.SelectedValue == "male")
                        {
                            if (filter == "")
                                filter = "Gender=True";
                            else
                                filter += " and Gender=True";
                        }
                        else
                        {
                            if (filter == "")
                                filter = "Gender=False";
                            else
                                filter += " and Gender=False";
                        }
                    }
                    if (tbx_CutomEmail.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Email LIKE '*" + tbx_CutomEmail.Text.Trim() + "*'") : filter + " and Email LIKE '*" + tbx_CutomEmail.Text.Trim() + "*'";
                    }
                    if (tbx_CutomMobile.Text.Trim() != "")
                    {
                        filter = (filter == "") ? ("Mobile LIKE '*" + tbx_CutomMobile.Text.Trim() + "*'") : filter + " AND Mobile LIKE '*" + tbx_CutomMobile.Text.Trim() + "*'";
                    }
                    if (ddl_CustomCatalog.SelectedValue != "0")
                    {
                        SqlDataReader dr_linkmantype = linkman.GetLinkmanTypeRelation();
                        //DataTable dt_linkmantype = UDS.Components.Tools.ConvertDataReaderToDataTable(dr_linkmantype);
                        //得到满足分类的联系人id
                        string ids = "";
                        while (dr_linkmantype.Read())
                        {
                            if (Convert.ToInt32(dr_linkmantype["TypeID"]) == Int32.Parse(ddl_CustomCatalog.SelectedValue))
                            {
                                ids += dr_linkmantype["CustomLinkmanID"] + ",";
                            }
                        }
                        if (ids != "")
                            ids = ids.Substring(0, ids.Length - 1);

                        filter = (filter == "") ? ("ID IN (" + ids + ")") : (filter + " AND ID IN (" + ids + ")");
                    }


                    dt_linkman = Tools.ConvertDataReaderToDataTable(dr_linkman);


                    try
                    {
                        dt_linkman.DefaultView.RowFilter = filter;
                        dgrd_Custom.DataSource = dt_linkman.DefaultView;
                        dgrd_Custom.DataBind();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(filter + "   " + ex.Message);
                    }


                    dgrd_Staff.Visible = false;
                    dgrd_Linkman.Visible = false;
                    dgrd_Custom.Visible = true;
                    break;
            }
        }
    }
}